#ifndef DEBUGGER_H
#define DEBUGGER_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <stdint.h>
#include <inttypes.h>

// 前向声明符号表
typedef struct symbol_table_s symbol_table_t;

// 前向声明DWARF类型
typedef struct dwarf_line_table_s dwarf_line_table_t;
typedef struct dwarf_variable_table_s dwarf_variable_table_t;

// 包含完整的DWARF类型定义
#include "dwarf.h"

// 调试器状态枚举
typedef enum {
    DEBUGGER_STOPPED,
    DEBUGGER_RUNNING,
    DEBUGGER_STEPPING,
    DEBUGGER_EXITED
} debugger_state_t;

// 断点结构
typedef struct {
    uintptr_t address;
    unsigned long original_data;
    int enabled;
} breakpoint_t;

// 调试器主结构
typedef struct {
    pid_t pid;
    debugger_state_t state;
    breakpoint_t *breakpoints;
    int breakpoint_count;
    int breakpoint_capacity;
    char program_path[1024];      // 被调试程序的路径
    symbol_table_t *symbol_table; // 符号表
    dwarf_line_table_t *dwarf_table; // DWARF行号表
    dwarf_variable_table_t *dwarf_variables; // DWARF变量信息表
    uintptr_t entry_point;        // 程序入口点地址
    uintptr_t base_address;       // 进程基地址
} debugger_t;

// 函数声明
debugger_t* debugger_create(void);
void debugger_destroy(debugger_t *dbg);
int debugger_launch(debugger_t *dbg, const char *program, char *const argv[]);
int debugger_attach(debugger_t *dbg, pid_t target_pid);
int debugger_detach(debugger_t *dbg);
int debugger_continue(debugger_t *dbg);
int debugger_step(debugger_t *dbg);
int debugger_set_breakpoint(debugger_t *dbg, uintptr_t address);
int debugger_remove_breakpoint(debugger_t *dbg, uintptr_t address);
void debugger_print_registers(debugger_t *dbg);
long debugger_read_memory(debugger_t *dbg, uintptr_t address);
int debugger_write_memory(debugger_t *dbg, uintptr_t address, long data);
void debugger_wait_for_signal(debugger_t *dbg);

// 符号解析相关函数
uintptr_t debugger_resolve_symbol(debugger_t *dbg, const char *symbol_name);
int debugger_load_symbols(debugger_t *dbg, const char *program_path);

// 栈帧结构
typedef struct stack_frame_s {
    uintptr_t return_address;      // 返回地址
    uintptr_t frame_pointer;        // 帧指针
    uintptr_t stack_pointer;        // 栈指针
    char function_name[256];       // 函数名
    char filename[256];            // 文件名
    unsigned int line_number;      // 行号
    struct stack_frame_s *next;    // 下一个栈帧
} stack_frame_t;

// 栈帧遍历相关函数
stack_frame_t* debugger_get_backtrace(debugger_t *dbg);
void debugger_free_backtrace(stack_frame_t *backtrace);
void debugger_print_backtrace(debugger_t *dbg);
int debugger_get_frame_info(debugger_t *dbg, int frame_index, char *function_name, size_t func_size, 
                           char *filename, size_t file_size, unsigned int *line_number);

// DWARF行号映射功能
int debugger_load_dwarf_info(debugger_t *dbg, const char *program_path);
uintptr_t debugger_find_address_by_line(debugger_t *dbg, const char *filename, unsigned int line);
int debugger_find_line_by_address(debugger_t *dbg, uintptr_t address, char *filename, size_t filename_size, unsigned int *line);

// 变量地址解析功能（增强版，支持类型信息）
uintptr_t debugger_find_variable_address(debugger_t *dbg, const char *variable_name);
void debugger_print_variable_address(debugger_t *dbg, const char *variable_name);
void debugger_print_variable_type(debugger_t *dbg, const char *variable_name);
dwarf_variable_info_t* debugger_get_variable_info(debugger_t *dbg, const char *variable_name);
int debugger_load_variable_info(debugger_t *dbg, const char *program_path);

// 高级DWARF解析函数
int debugger_load_variable_info_advanced(debugger_t *dbg, const char *program_path);
int debugger_reload_variable_info_with_dwarfdump(debugger_t *dbg);

// 反汇编功能
int debugger_disassemble(debugger_t *dbg, uintptr_t address, size_t count);
int debugger_disassemble_current(debugger_t *dbg, size_t count);
int debugger_disassemble_function(debugger_t *dbg, const char *function_name);

// libdwarf增强功能声明
int dwarf_libdwarf_init(const char* filename);
int dwarf_get_variable_type_enhanced(const char* var_name, char* type_info, size_t type_info_len, const char* program_path);

// 进程内存映射功能
int debugger_print_proc_maps(debugger_t *dbg);

#endif // DEBUGGER_H